package cn.stylefeng.guns.modular.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.order.entity.OrderTransportDetailed;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportDetailedParam;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportDetailedResult;
import cn.stylefeng.guns.modular.order.service.OrderTransportDetailedService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 货运订单明细表控制器
 *
 * @author zhucx
 * @Date 2019-07-18 17:01:28
 */
@Controller
@RequestMapping("/orderTransportDetailed")
public class OrderTransportDetailedController extends BaseController {

    private String PREFIX = "/order/orderTransportDetailed";

    @Autowired
    private OrderTransportDetailedService orderTransportDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    /**
     * 跳转到主页面
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("")
    public String index(OrderTransportDetailedParam orderTransportDetailedParam,Model model) {
    	model.addAttribute("transportNumber", orderTransportDetailedParam.getTransportNumber());
        return PREFIX + "/orderTransportDetailed.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/add")
    public String add(@RequestParam("transportNumber") String transportNumber,
    		@RequestParam("customerId") Long customerId,Model model) {
    	model.addAttribute("transportNumber", transportNumber);
    	model.addAttribute("customerId", customerId);
        return PREFIX + "/orderTransportDetailed_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("transportNumber") String transportNumber,
    		@RequestParam("customerId") Long customerId,Model model) {
    	model.addAttribute("transportNumber", transportNumber);
    	model.addAttribute("customerId", customerId);
        return PREFIX + "/orderTransportDetailed_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderTransportDetailedParam orderTransportDetailedParam) {
    	orderTransportDetailedParam.setNotnum(orderTransportDetailedParam.getNum());
    	orderTransportDetailedParam.setNotvolume(orderTransportDetailedParam.getTransportPutvolume());
    	OrderTransportDetailedResult otdr = orderTransportDetailedService.findBySpec(orderTransportDetailedParam);
    	if (otdr!=null) {
    		ResponseData data = new ResponseData();
    		data.setSuccess(false);
    		data.setMessage("请勿重复添加商品");
    		return data;
		}
    	String goodsName = orderTransportDetailedParam.getGoodsName();
    	//解决参数中的 英文括号
    	if (goodsName!=null&&goodsName!="") {
    		goodsName = goodsName.replace("& #40;", "(");
    		goodsName = goodsName.replace("& #41;", ")");
    		orderTransportDetailedParam.setGoodsName(goodsName);
    	}
    	String goodsNo = orderTransportDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		orderTransportDetailedParam.setGoodsNo(goodsNo);
    	}
        this.orderTransportDetailedService.add(orderTransportDetailedParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderTransportDetailedParam orderTransportDetailedParam) {
    	String goodsName = orderTransportDetailedParam.getGoodsName();
    	//解决参数中的 英文括号
    	if (goodsName!=null&&goodsName!="") {
    		goodsName = goodsName.replace("& #40;", "(");
    		goodsName = goodsName.replace("& #41;", ")");
    		orderTransportDetailedParam.setGoodsName(goodsName);
    	}
    	String goodsNo = orderTransportDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		orderTransportDetailedParam.setGoodsNo(goodsNo);
    	}
    	orderTransportDetailedParam.setNotnum(orderTransportDetailedParam.getNum());
    	orderTransportDetailedParam.setNotvolume(orderTransportDetailedParam.getTransportPutvolume());
        this.orderTransportDetailedService.update(orderTransportDetailedParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderTransportDetailedParam orderTransportDetailedParam) {
        this.orderTransportDetailedService.delete(orderTransportDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderTransportDetailedParam orderTransportDetailedParam) {
        OrderTransportDetailed detail = this.orderTransportDetailedService.getById(orderTransportDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2019-07-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderTransportDetailedParam orderTransportDetailedParam,String condition ) {
        return this.orderTransportDetailedService.findPageBySpec(orderTransportDetailedParam,condition);
    }

}


